﻿jQuery('#btnEmbed').click(function () {
    //var content = genHTMLList(false);
    var content = jQuery('#preview').html();
    getRadWindow().close(content);
});

//Function editor
if (window.attachEvent) {
    window.attachEvent("onload", initDialog);
}
else if (window.addEventListener) {
    window.addEventListener("load", initDialog, false);
}

function getRadWindow() {
    if (window.radWindow) {
        return window.radWindow;
    }
    if (window.frameElement && window.frameElement.radWindow) {
        return window.frameElement.radWindow;
    }
    return null;
}

function initDialog() {
    if (getRadWindow() != null) {
        var clientParameters = getRadWindow().ClientParameters;
        if (typeof clientParameters.html != 'undefined') {
            jQuery('#widthBox').val(clientParameters.width);
            jQuery('#numCols').val(clientParameters.cols);
            jQuery('#template').val(clientParameters.templ);
            jQuery('#paddingLi').val(clientParameters.ipad);
            jQuery('#type_img').attr('checked', clientParameters.disImg.toLowerCase() == "true");
            jQuery('#type_title').attr('checked', clientParameters.disTitle.toLowerCase() == "true");
            jQuery('#type_sapo').attr('checked', clientParameters.disSapo.toLowerCase() == "true");
            
            jQuery("<ul>" + clientParameters.html + "</ul>").find('li').each(function () {
                var objLink = jQuery(this).find('a.link');
                jQuery('#selected_columns').append(new Option(objLink.text(), parseInt(jQuery(this).attr('id'))));
            });
            jQuery('#btnPreview').trigger('click');
        }
    } else {
    }
}

//var obj = {};
//obj.product = [];
//for (var i = 0; i < someObj.length; i++) {
//obj.product.push[{ "attribute": someObj[i]}]
//}
//Result: {"product":[{"attribute":"value"}, {"attribute":"value"}]}

jQuery(document).ready(function ($) {
    var pathAjax = basepath + 'Ajax.aspx';
    var elTextSearch = '#keyword_text';

    //Defined button Options
    $('#addSel').click(function () {
        return !$('#available_columns option:selected').remove().appendTo('#selected_columns');
    });
    $('#removeSel').click(function () {
        return !$('#selected_columns option:selected').remove().appendTo('#available_columns');
    });

    $('#mup').click(function () {
        $('#selected_columns option:selected').each(function () {
            $(this).insertBefore($(this).prev());
        });
    });

    $('#mdown').click(function () {
        $('#selected_columns option:selected').each(function () {
            $(this).insertAfter($(this).next());
        });
    });

    $(elTextSearch).keyup(function (e) {
        var key = e.keyCode || e.which;
        if (key === 13) searchContent();
    });

    $('form').keypress(function (e) {
        if (e.which === 13) {
            return false;
        }
    });

    function searchContent() {
        var keyword = $(elTextSearch).val();
        if (keyword.length >= 2) {
            displayIconAjax($('#loading'), true);
            $.ajax({
                url: pathAjax,
                dataType: 'json',
                data: {
                    p: 'Content',
                    act: 'load',
                    keyword: keyword,
                    ignoreIds: dataOptionsRight(),
                    noneCache: 'true'
                },
                success: function (data) {
                    displayIconAjax($('#loading'), false);
                    $('#available_columns').empty();
                    if (data.label.length) {
                        for (var i in data.label) {
                            $('#available_columns').append('<option value="' + data.id[i] + '" url="' + data.url[i] + '">' + data.label[i] + '</option>');
                        }
                    }
                }
            });
        }
    }

    $('#btnPreview').click(function () {
        var val = $('#template').val();
        var ids = dataOptionsRight();
        console.log(ids);
        if (!ids) {
            alert('Bạn chưa chọn bài viết nào!');
            return;
        }
        useAjax({
            type: 'POST',
            data: {
                typePage: 'Custom',
                dir_page: 'Box',
                p: 'Box',
                templ: val,
                show_img: jQuery('#type_img').attr('checked'),
                show_title: jQuery('#type_title').attr('checked'),
                show_sapo: jQuery('#type_sapo').attr('checked'),
                widthBox: jQuery('#widthBox').val(),
                numCols: jQuery('#numCols').val(),
                paddingLi: jQuery('#paddingLi').val(),
                listIds: ids
            },
            success: function (data) {
                jQuery('#preview').html(data);
                jQuery("#list_order").sortable({ opacity: 0.6, cursor: 'move',
                    update: function (event, ui) {
                        var order = jQuery(this).sortable('toArray');
                        console.log(order);
                    }
                });
                //http://stackoverflow.com/questions/1672182/jquery-sortable-live
                //http://jqueryui.com/demos/sortable/default.html
            }
        });
    });
    //.trigger('click');

    function dataOptionsRight() {
        var s = '';
        var arr = new Array();
        arr.push(0);
        $('#selected_columns option').each(function () {
            arr.push($(this).val());
        });
        s = arr.join(',');
        return s;
    }

    function displayIconAjax(el, status) {
        if (status) $(el).show();
        else $(el).hide();
    }
});